<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式锁原理与实践 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-box {
            position: relative;
        }
        .highlight-box::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 4px;
            background: linear-gradient(to bottom, #667eea, #764ba2);
            border-radius: 4px 0 0 4px;
        }
        .drop-cap::first-letter {
            font-size: 3.5em;
            font-weight: bold;
            float: left;
            line-height: 0.8;
            margin-right: 0.2em;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 serif">分布式锁原理与实践</h1>
                <p class="text-xl md:text-2xl max-w-3xl leading-relaxed mb-8">在分布式系统中保障数据一致性的关键机制</p>
                <div class="flex space-x-4">
                    <a href="#intro" class="px-6 py-3 bg-white text-indigo-700 rounded-lg font-medium hover:bg-opacity-90 transition">开始探索</a>
                    <a href="#solutions" class="px-6 py-3 bg-indigo-800 bg-opacity-50 text-white rounded-lg font-medium hover:bg-opacity-70 transition">解决方案</a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-16">
        <!-- Introduction Section -->
        <section id="intro" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-600 mr-4"></div>
                <h2 class="text-3xl font-bold text-gray-800 serif">为什么需要分布式锁</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-lock text-indigo-600 text-lg"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">本地锁的局限性</h3>
                    </div>
                    <p class="text-gray-700 mb-4">在单机环境中，我们可以使用本地锁（如Java中的synchronized或ReentrantLock）来保证线程安全。然而，在分布式环境下，本地锁只能保证当前JVM进程内的线程安全，无法跨越多个服务器节点。</p>
                    
                    <div class="highlight-box pl-6 py-2 bg-gray-50 rounded my-4">
                        <p class="text-gray-700 font-medium">关键问题：分布式系统由多个独立节点组成，本地锁无法跨节点生效，导致数据一致性问题。</p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-network-wired text-purple-600 text-lg"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">分布式锁的必要性</h3>
                    </div>
                    <p class="text-gray-700 mb-4">分布式锁是一种跨进程、跨服务器的互斥机制，能够在分布式环境中实现对共享资源的排他性访问。它需要满足以下基本要求：</p>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>互斥性：同一时刻只能有一个客户端持有锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>避免死锁：锁必须有超时机制或自动释放策略</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>高可用：锁服务需要具备容错能力</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-20 bg-white rounded-xl shadow-md p-6">
            <h3 class="text-xl font-bold text-gray-800 mb-4 serif">本地锁 vs 分布式锁</h3>
            <div class="mermaid">
                graph LR
                    A[单机环境] --> B(本地锁)
                    B --> C[线程安全]
                    D[分布式环境] --> E(分布式锁)
                    E --> F[跨节点同步]
                    style A fill:#e2f0ff,stroke:#4a7dc6
                    style B fill:#d5e8ff,stroke:#4a7dc6
                    style D fill:#e2e1ff,stroke:#6b5ce7
                    style E fill:#d5d4ff,stroke:#6b5ce7
            </div>
        </section>

        <!-- Solutions Section -->
        <section id="solutions" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-600 mr-4"></div>
                <h2 class="text-3xl font-bold text-gray-800 serif">分布式锁解决方案</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Redis Solution -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-red-600 text-white p-4 flex items-center">
                        <i class="fab fa-reddit-alien text-2xl mr-3"></i>
                        <h3 class="text-xl font-bold">Redis 实现方案</h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">基于Redis的SETNX命令实现分布式锁，结合Lua脚本保证原子性操作。</p>
                        
                        <div class="bg-gray-800 rounded-lg p-4 mb-4 overflow-x-auto">
                            <code class="text-green-400 text-sm">
                                SET resource_name my_random_value NX PX 30000
                            </code>
                        </div>
                        
                        <h4 class="font-bold text-gray-800 mb-2">核心特性：</h4>
                        <ul class="space-y-2 text-gray-700 mb-4">
                            <li class="flex items-start">
                                <i class="fas fa-bolt text-yellow-500 mt-1 mr-2"></i>
                                <span>高性能：基于内存操作，响应迅速</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-shield-alt text-blue-500 mt-1 mr-2"></i>
                                <span>自动过期：通过EXPIRE防止死锁</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-random text-purple-500 mt-1 mr-2"></i>
                                <span>随机值：确保只有锁持有者能释放</span>
                            </li>
                        </ul>
                        
                        <a href="https://www.yuque.com/jtostring/qiwsg9/fq93ar" class="text-red-600 font-medium hover:underline inline-flex items-center">
                            了解更多 Redisson 实现细节
                            <i class="fas fa-arrow-right ml-2"></i>
                        </a>
                    </div>
                </div>
                
                <!-- Zookeeper Solution -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-blue-600 text-white p-4 flex items-center">
                        <i class="fas fa-paw text-2xl mr-3"></i>
                        <h3 class="text-xl font-bold">Zookeeper 实现方案</h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">利用Zookeeper的临时顺序节点特性实现分布式锁，确保强一致性。</p>
                        
                        <div class="bg-gray-800 rounded-lg p-4 mb-4 overflow-x-auto">
                            <code class="text-green-400 text-sm">
                                create -e -s /lock/resource-  # 创建临时顺序节点
                            </code>
                        </div>
                        
                        <h4 class="font-bold text-gray-800 mb-2">核心特性：</h4>
                        <ul class="space-y-2 text-gray-700 mb-4">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>强一致性：基于ZAB协议保证</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-user-clock text-indigo-500 mt-1 mr-2"></i>
                                <span>临时节点：客户端断开自动释放</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-list-ol text-orange-500 mt-1 mr-2"></i>
                                <span>顺序节点：实现公平锁机制</span>
                            </li>
                        </ul>
                        
                        <div class="text-gray-500 text-sm">
                            <i class="fas fa-info-circle mr-2"></i>
                            适用于对一致性要求极高的场景，如金融交易
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Table -->
        <section class="mb-20 bg-white rounded-xl shadow-md overflow-hidden">
            <div class="p-6 border-b">
                <h3 class="text-xl font-bold text-gray-800 serif">方案对比</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Redis</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Zookeeper</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">性能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span class="inline-flex items-center text-green-600">
                                    <i class="fas fa-bolt mr-2"></i> 高
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">中等</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">一致性</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">最终一致</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span class="inline-flex items-center text-blue-600">
                                    <i class="fas fa-check-circle mr-2"></i> 强一致
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">实现复杂度</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">简单</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">复杂</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">适用场景</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">高并发、允许短暂不一致</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">强一致性要求场景</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-indigo-50 rounded-xl p-8">
            <h3 class="text-2xl font-bold text-gray-800 mb-6 serif">选择指南</h3>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="drop-cap">
                    <p class="text-gray-700">在分布式系统设计中，选择何种锁方案需要权衡性能与一致性的需求。Redis方案适合高并发但对一致性要求不苛刻的场景，而Zookeeper则适合金融交易等对一致性要求极高的系统。</p>
                </div>
                <div>
                    <p class="text-gray-700 mb-4">现代分布式系统往往会根据业务特点采用混合策略，如：</p>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                            <span>多级缓存系统使用Redis锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                            <span>支付系统使用Zookeeper锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-2"></i>
                            <span>结合使用实现读写分离锁</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col items-center">
                <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                <p class="mb-6">探索技术世界的精彩角落</p>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition flex items-center">
                    访问语雀主页 <i class="fas fa-external-link-alt ml-1 text-sm"></i>
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC',
        });
    </script>
</body>
</html>